package com.wenshi.web.controller.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wenshi.common.utils.StringUtils;
import com.wenshi.system.domain.Alarm;
import com.wenshi.system.domain.HydrogenProductionRates;
import com.wenshi.system.domain.SysDevice;
import com.wenshi.system.service.IHydrogenProductionRateService;
import com.wenshi.system.service.ISysAlarmService;
import com.wenshi.system.service.ISysDeviceService;
import com.wenshi.web.socket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class DealSchedule {

    @Autowired
    private ISysAlarmService iSysAlarmService;
    @Autowired
    private IHydrogenProductionRateService rateService;
    @Autowired
    private ISysDeviceService iSysDeviceService;
//    @Scheduled(fixedDelay = 600000L)
    public void dealHydrogenRateThread(){
        int size= WebSocket.rateMap.size();
        if (!WebSocket.rateMap.isEmpty()&&size>0){
            log.info("当前待处理的产氢率集合长度：{}",size);
            Double productionRateAdd=0.0;
            String deviceIp=null;
            for (HydrogenProductionRates hydrogenProductionRates : WebSocket.rateMap) {
                deviceIp = hydrogenProductionRates.getDeviceIp();
                productionRateAdd+= hydrogenProductionRates.getProductionRate();
                WebSocket.rateMap.poll();
            }
            QueryWrapper<SysDevice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("device_ip",deviceIp);
            SysDevice one = iSysDeviceService.getOne(queryWrapper);
            HydrogenProductionRates hydrogenProductionRates = new HydrogenProductionRates();
            hydrogenProductionRates.setDeviceId(one.getDeviceId());
            hydrogenProductionRates.setTimestamp(new Date());
            Double productionRate=productionRateAdd/size;
            BigDecimal result = new BigDecimal(productionRate).setScale(2, RoundingMode.HALF_UP);
            double productionRateResult = result.doubleValue();
            hydrogenProductionRates.setProductionRate(productionRateResult);
            hydrogenProductionRates.setDeviceIp(deviceIp);
//            boolean save = rateService.save(hydrogenProductionRates);
//            if (save) {
//                log.info("当前这分钟采集到的产氢率：{}",hydrogenProductionRates);
//            }
        }
        int afterSize= WebSocket.rateMap.size();
        log.info("当前剩余的产氢率集合长度：{}",afterSize);
    }

//    @Scheduled(fixedDelay = 10000L)
    public void dealAlarmThread(Long deviceId){
//        SysDevice byDeviceIp = iSysDeviceService.getByDeviceIp(deviceIp);
//        Long deviceId = byDeviceIp.getDeviceId();
        QueryWrapper<Alarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("alarm_status",false).eq("device_id",deviceId);
        //查出当前待处理的告警集合
        List<Alarm> list = iSysAlarmService.list(queryWrapper);
        int size = WebSocket.dealMap.size();
        if (!WebSocket.dealMap.isEmpty()&&size>0){
            log.info("当前待处理的告警长度：{}",size);
            for (Alarm alarm : WebSocket.dealMap) {
                QueryWrapper<Alarm> wrapper = new QueryWrapper<>();
                wrapper.eq("alarm_type",alarm.getAlarmType()).eq("alarm_status",alarm.isAlarmStatus())
                        .eq("device_id",alarm.getDeviceId()).eq("alarm_name",alarm.getAlarmName());
                Alarm one = iSysAlarmService.getOne(wrapper);
                if (StringUtils.isNull(one)){
                    iSysAlarmService.save(alarm);
                    log.info("插入一条新告警,内容是:{}",alarm.toString());
                }else {
                    log.info("已存在同一告警,告警id：{},不存储",one.getAlarmId());
                    list.remove(one);
                }
                WebSocket.dealMap.poll();
            }
            for (Alarm alarm : list) {
                alarm.setAlarmStatus(true);
//                iSysAlarmService.updateById(alarm);
            }
        }
    }
}
